import React, { useEffect } from 'react';
import Cookies from 'js-cookie';
import BasicLayout from './biz_layouts';
import { lz, request, http } from '@zny/utils';
import { useStore, setLocalStorage, useLocalStorage } from '@zny/hooks';
import SubAppLayout from './SubAppLayout';
import { UI } from '../utils/UI';
import { WebVariable } from '@/service/WebVariable';
import { Session } from '@/service/Session';

const LayoutIndex: React.FC<{}> = (props: any) => {
  const globalSettings = localStorage.getItem('userInfo');
  let projectId = '';
  if (globalSettings) {
    projectId = JSON.parse(lz(globalSettings, true))._LoginProjectId;
  }

  console.log(
    projectId,
    '..................................获取到的项目ID..................................',
  );
  const [defaultSettings, setDefaultSettings] = useStore('defaultSettings', null);
  const checkAuthToken = (defaultSettings: any) => {
    // const znyToken = Cookies.get('znyToken') as string; // znyToken 的值可能等于 {} (张主工反映)
    const znyToken = JSON.stringify({
      expiresIn: 86400,
      refreshTokenExpiresIn: 604800,
      license: 'powered+by+microservices',
      authority: '',
      permission: '',
      avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
      accessToken:
        'eyJ0eXAiOiJKc29uV2ViVG9rZW4iLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpc3N1c2VyIiwiYXVkIjoiYXVkaWVuY2UiLCJ1c2VyX2lkIjoiOEE1Qjg4RDYtNkRDNy1FMkY0LTAxNkUtMTA0RTYxQTgwMDNCIiwidG9rZW5fdHlwZSI6ImFjY2Vzc190b2tlbiIsImNsaWVudF9pZCI6InN3b3JkIiwiZXhwIjoxNjA0MDIyMzE1LCJuYmYiOjE2MDM5MzU5MTV9.TxIV9M2RHMqVfTaxURn7FQ1NKOEtN6AQr_Is0wPwTIc',
      tokenType: 'bearer',
      userName: '',
      account: '',
      refreshToken:
        'eyJ0eXAiOiJKc29uV2ViVG9rZW4iLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpc3N1c2VyIiwiYXVkIjoiYXVkaWVuY2UiLCJ1c2VyX2lkIjoiOEE1Qjg4RDYtNkRDNy1FMkY0LTAxNkUtMTA0RTYxQTgwMDNCIiwidG9rZW5fdHlwZSI6InJlZnJlc2hfdG9rZW4iLCJjbGllbnRfaWQiOiJzd29yZCIsImV4cCI6MTYwNDU0MDcxNSwibmJmIjoxNjAzOTM1OTE1fQ.2yafh-7bIpfTsfK03WsvQpHA2eRE0XN0pkBHcJdTCkc',
    });
    const cookie_UserId = '8a5b88d66dc7e2f4016e104e61a8003b';
    // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    // const znyToken = JSON.stringify({
    //   expiresIn: 86400,
    //   refreshTokenExpiresIn: 604800,
    //   license: 'powered+by+microservices',
    //   authority: '',
    //   permission: '',
    //   avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
    //   accessToken:
    //     'eyJ0eXAiOiJKc29uV2ViVG9rZW4iLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpc3N1c2VyIiwiYXVkIjoiYXVkaWVuY2UiLCJ1c2VyX2lkIjoiOEE1Qjg4RDYtNkRDNy1FMkY0LTAxNkUtMTA0RTYxQTgwMDNCIiwidG9rZW5fdHlwZSI6ImFjY2Vzc190b2tlbiIsImNsaWVudF9pZCI6InN3b3JkIiwiZXhwIjoxNjAxNDM3MDg5LCJuYmYiOjE2MDEzNTA2ODl9.DV7Y9leutZouasautvTtDQlVJdJSxVKW5rjxTTS0xB8',
    //   tokenType: 'bearer',
    //   userName: '',
    //   account: '',
    //   refreshToken:
    //     'eyJ0eXAiOiJKc29uV2ViVG9rZW4iLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpc3N1c2VyIiwiYXVkIjoiYXVkaWVuY2UiLCJ1c2VyX2lkIjoiOEE1Qjg4RDYtNkRDNy1FMkY0LTAxNkUtMTA0RTYxQTgwMDNCIiwidG9rZW5fdHlwZSI6ImFjY2Vzc190b2tlbiIsImNsaWVudF9pZCI6InN3b3JkIiwiZXhwIjoxNjAxNDM3MDg5LCJuYmYiOjE2MDEzNTA2ODl9.DV7Y9leutZouasautvTtDQlVJdJSxVKW5rjxTTS0xB8',
    // });
    //
    // const cookie_UserId = '8a5b88d66dc7e2f4016e104e61a8003b';

    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    let pathname: string = '';

    if (process.env.NODE_ENV == 'development') {
      pathname = 'http://10.219.86.138:8001';
    } else {
      pathname = location.origin;
    }

    if (znyToken === undefined || cookie_UserId == undefined || znyToken.length < 2) {
      UI.error('单点登录读取Cookie失败');
      setTimeout(() => {
        Session.logout();
        //window.open("http://portal.cs.msdi.cn/r1/SignOnServlet?FromUrl=" + pathname, '_parent');
      }, 1000);
    } else {
      const deCodeCookie = decodeURIComponent(znyToken);
      const obj = JSON.parse(deCodeCookie);
      // const { _LoginProjectId, _TenantId } = defaultSettings;
      const { accessToken } = obj;

      let headers = {
        _LoginProjectId: projectId || '',
        _SysId: WebVariable.sysId,
        _TenantId: WebVariable.tenantId,
        token: accessToken,
        Authorization: accessToken,
      };
      request.extendOptions({
        headers,
      });
      http.extendOptions({
        headers,
      });
    }
  };

  useEffect(() => {
    setDefaultSettings(null);
    http('/rjApi/data/getByKey', {
      params: { key: 'defaultSettings' },
    }).then(res => {
      if (!res || !res.success) {
        console.log(`获取默认配置失败：${res.message}`);
        return;
      }
      checkAuthToken(JSON.parse(res.data.value));
      setDefaultSettings(JSON.parse(res.data.value));
      setLocalStorage('globalSettings', JSON.parse(res.data.value));
    });
  }, []);

  if (window.__POWERED_BY_QIANKUN__) {
    return defaultSettings && <SubAppLayout {...props} />;
  }
  return <>{defaultSettings && <BasicLayout {...props} />}</>;
};

export default LayoutIndex;
